<?php
/**
 * Created by PhpStorm.
 * User: work
 * Date: 18-8-11
 * Time: 上午11:23
 */

namespace PddWechat\Library;


class ClassLibrary
{
    public static function classCopy($source, $object){
        $array = json_decode( json_encode($source), true );
        $reflect = new \ReflectionObject($object);
        $properties = $reflect->getProperties();
        foreach ($properties as $property) {
            $propertyName = $property->name;
            if( isset($array[$propertyName]) && $array[$propertyName] != null) {
                $object->$propertyName = self::getValue($object, $propertyName, $array[$propertyName]);
            }
        }
        return $object;
    }


    public static function getValue( $className, $propertyName, $value ) {
        $reflectionProperty = new \ReflectionProperty($className, $propertyName);
        $comment = $reflectionProperty->getDocComment();
        $type = \Dai\Framework\Library\Annotations::getCommentValue($comment, 'type' );

        if( $type === "Int") {
            return intval($value);
        }elseif($type === "Double") {
            return doubleval($value);
        }elseif($type === "Json") {
            return json_decode($value);
        }elseif($type === "Time" && is_integer($value)) {
            if( $value > 100000000000) {
                $value /= 1000;
            }
            return  date('Y-m-d H:i:s', $value);
        }else {
            return trim($value);
        }
    }
}